
local locplayer = ac.player.self
local w,h = 300,300
local ui = class.panel:builder 
{
    x = (1920 - w)/2,
    y = 300,
    w = w,
    h = h,
    show_flag = false,
    alpha = 0.99,
    level = 99,
    fdf = 'tooltip_backdrop0',
}
ui:set_level(99)
ui.title = ui:add_text('|cffffff00替换天赋|r',0,5,nil,30,12,5)
ui.icon = ui:add_card_button((ui.w-64)/2,40,64,64)


local normal_icon = [[core/emptyinvslot1.blp]]
local w1,h1 = 64,64
local x1 = 15
local y1 = 140
ui.buttons = {}

for a=1,2 do
    for b=1,4 do
        local x = (b-1) * (w1 + 5) + x1
        local y = (a-1) * (h1 + 1) + y1
        local panel = ui:add_card_button(x,y,w1,h1)
        panel.index = #ui.buttons + 1
        table.insert(ui.buttons,panel)
    end
end

local empty = {}
function ui:update()
    local hero = locplayer.hero
    local data = hero and hero._card_list or empty
    local temp = locplayer:get_data('替换抽卡名')
    self.icon:set_data(temp)
    for i,button in ipairs(self.buttons) do
        local skill = data[i]
        button:set_data(skill)
    end
end

function ui:on_button_mouse_enter(button)
    local skill = button.skill
    if skill then
        button:tooltip(skill:get_title(),skill:get_tip())
    end
end

function ui:on_button_clicked(button)
    if button==self.esc then
        self:hide()
        return
    end
    ac.sync('确定替换抽卡',button.index)
end

ac.sync:event '确定替换抽卡'(function(_,player,index)
    local hero = player.hero
    local temp = player:get_data('替换抽卡名')
    if hero and temp and index>=1 and index<=12 then
        player:set_data('替换抽卡名',nil)
        hero:add_card(temp.name,index,{pool = temp.pool})
        hero:notify('英雄-完成抽卡替换',hero)
    end
    if player:is_self() then
        ui:hide()
    end
end)

ac.game:event '英雄-替换抽卡'(function(_,hero,temp)
    local player = hero.owner
    player:set_data('替换抽卡名',temp)
    if player:is_self() then
        ui:update()
        ui:show()
    end
end)

ac.game:event '玩家-刷新UI'(function(_,player)
    if player:is_self() and ui:is_absolute_show() then
        ui:update()
    end
end)

